package com.szy.system.common.util;

import org.springframework.data.domain.Page;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : zhenyun.su
 * @comment : 配合angular ant框架ngalain使用
 * @since : 2020-7-6
 */
@Deprecated
public final class NgAlainUtils {
    public static final String ng_alain_total = "total";
    public static final String ng_alain_list = "list";

    public static <T> Map<String, Object> getResult(Integer total, List<T> list) {
        if (ObjectUtils.isEmpty(list)) {
            return getNull();
        }
        Map<String, Object> map = new HashMap<>();
        if (total <= list.size()) {
            total = list.size();
        }
        map.put(ng_alain_total, total);
        map.put(ng_alain_list, list);
        return map;
    }

    public static <T> Map<String, Object> getResult(List<T> list) {
        if (ObjectUtils.isEmpty(list)) {
            return getNull();
        }
        Map<String, Object> map = new HashMap<>();
        map.put(ng_alain_total, list.size());
        map.put(ng_alain_list, list);
        return map;
    }

    public static <T> Map<String, Object> getResult(T obj) {
        if (obj == null) {
            return getNull();
        }
        Map<String, Object> map = new HashMap<>();
        map.put(ng_alain_total, 1);
        map.put(ng_alain_list, obj);
        return map;
    }

    public static Map<String, Object> getResult(Page page) {
        if (ObjectUtils.isEmpty(page)) {
            return getNull();
        }
        Map<String, Object> map = new HashMap<>();
        map.put(ng_alain_total, page.getTotalElements());
        map.put(ng_alain_list, page.getContent());
        return map;
    }

    public static Map<String, Object> getNull() {
        Map<String, Object> map = new HashMap<>();
        map.put(ng_alain_total, 0);
        return map;
    }

}
